<?php
/**
 * This file contains the class / object that is messaging
 * between nodes.
 * 
 * @author Gustav Knutsson
 */
class jdNetworkMessage{
	// Holds the recipient address
	private $recipient_url;
	
	// Holds the result, if no result, then null
	private $result = null;
	
	/**
	 * Sets the recipient node address (URL) 
	 * @param String $url
	 * 
	 * @author Gustav Knutsson
	 */
	public function setRecipient($url){
		$this->recipient_url = $url;
	}
	
	/**
	 * Sends fields containing data to the recipient
	 * @param Array $fields
	 * 
	 * @author Gustav Knutsson
	 */
	public function _send($fields){
		foreach($fields as $key => $value){
			$fields_string .= $key.'='.$value.'&';
		}
		rtrim($fields_string,'&');
		
		$ch = curl_init();
		
		//set the url, number of POST vars, POST data
		curl_setopt($ch,CURLOPT_URL,$this->recipient_url);
		curl_setopt($ch,CURLOPT_POST,count($fields));
		curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		
		//execute post
		$this->result = curl_exec($ch);
		
		//close connection
		curl_close($ch);
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	public function _answer(){
		return $this->result;
	}
}
?>